package com.wj.jiangan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.Product;
import com.wj.jiangan.pojo.Sort;
import com.wj.jiangan.pojo.vo.SortAndProductQueryVo;
import com.wj.jiangan.service.ISortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@RestController
@RequestMapping("sort")
public class SortController {

    @Autowired
    public ISortService sortService;

    /**
     * 返回所有存在的产品类别
     * @return
     */
    @GetMapping("sortExistList")
    public List<Sort> getAllExistSort() {
        return sortService.getAllExistSort();
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("sortList")
    public List<Sort> selectSort() {
        return sortService.selectAllSort();
    }

    /**
     * 分页
     * 类别分页
     *
     * @param page
     * @return
     */
    @GetMapping("findSortPage")
    public Map<String, Object> findpageSortNum(Page<Sort> page) {
        IPage<Sort> sortIPage = sortService.findSortByPage(page);
        List<Map<String, Object>> list = new ArrayList<>();
        sortIPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("sid", o.getSId());
            map.put("sname", o.getSName());
            map.put("sremark", o.getSRemark());
            map.put("sis", o.getSIs());
            list.add(map);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "");
        result.put("count", sortIPage.getTotal());
        result.put("data", list);

        return result;
    }

    /**
     * 添加
     *
     * @param sort
     * @return
     */

    @PostMapping("saveSort")
    @ResponseStatus(value = HttpStatus.CREATED)
    public int saveSort(Sort sort) {
        System.out.println(sort);

        return sortService.saveSort(sort);
    }

    /**
     * 根据名字查询
     *
     * @param sort
     * @return
     */
    @GetMapping("sortByName")
    public List<Sort> selectSortByName(Sort sort) {
        return sortService.selectSortByName(sort);
    }

    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    @GetMapping("sortname")
    public Sort sortName(@RequestParam String name) {
        return sortService.sortByName(name);
    }
    @RequestMapping("/querySortByName")
    public Map querySortByName(String sName){
        QueryWrapper<Sort> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("s_name",sName);
        List<Sort> sorts = sortService.querySortByName(productQueryWrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("ss",sorts.size());
        return map;
    }
    /**
     * @return
     */
    @GetMapping("sortAndProduct")
    public List<SortAndProductQueryVo> findSortAndProductInfo() {
        return sortService.findSortAndProductInfo();
    }

    /**
     * //产品类别和商品名称进行精确的查询
     *
     * @return
     */
    @GetMapping("sortAndProductAll")
    public List<SortAndProductQueryVo> findSortAndProductInfoAll() {
        return sortService.findSortAndProductIndoAll();
    }


    /**
     * //分页产品类别和商品名称进行精确的查询
     *
     * @return
     */
    @GetMapping("sortAndProductAllpage")
    public Map<String, Object> findSortAndProductInfoAllPage(Page page,
                                                             @RequestParam(required = false) String sname,
                                                             @RequestParam(required = false) String pname) {

        Map<String, Object> param = new HashMap<>();
        if (!StringUtils.isEmpty(sname)) {
            param.put("sname", sname);
        }
        if (!StringUtils.isEmpty(pname)) {
            param.put("pname", pname);
        }

        IPage<SortAndProductQueryVo> spAllPage = sortService.findSortAndProductInfoAllPage(page, param);


        List<Map<String, Object>> list = new ArrayList<>();
        spAllPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("pid", o.getProduct().getPId());
            map.put("pname", o.getProduct().getPName());
            map.put("parea", o.getProduct().getPArea());
            map.put("pml", o.getProduct().getPMl());
            map.put("sname", o.getSort().getSName());
            list.add(map);
        });

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", spAllPage.getTotal());
        result.put("data", list);
        System.out.println(list);
        return result;
    }

    //下架类别查询
    @GetMapping("uppxia")
    public Map<String, Object> sortByIsUppxia(Page page, @RequestParam(required = false) Integer sis) {
        Map<String, Object> param = new HashMap<>();
        if (!StringUtils.isEmpty(sis)) {
            param.put("sis", sis);
        }

        IPage<Sort> suAllPage = sortService.sortByIsUppxia(page, param);

        List<Map<String, Object>> list = new ArrayList<>();
        suAllPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("sid", o.getSId());
            map.put("sname", o.getSName());
            map.put("sremark", o.getSRemark());
            map.put("sis", o.getSIs());
            list.add(map);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", suAllPage.getTotal());
        result.put("data", list);
        System.out.println(list);
        return result;
    }

    //下架
    @PostMapping("editSort")
    public void editSort(@RequestParam Integer sid) {
        sortService.uppxia(sid);
    }
    //上架

    @PostMapping("uppshangSort")
    public void editshangSort(@RequestParam Integer sid) {
        sortService.uppshang(sid);
    }

    //库存管理三级联动
    @GetMapping("/{sid}")
    public List<Sort> findsortBysid(@PathVariable int sid) {
        return sortService.findsortvoAll(sid);
    }

}
